﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace Simulation.Framework
{
    public class TimerManager: ManagerBehavior
    {
        private List<Timer> timerPool = new List<Timer>();

        private void FixedUpdate()
        {
            FixedUpdateTimer();
        }

        private void Update()
        {
            UpdateTimer();
        }

        public override void Init()
        {
            timerPool = new List<Timer>();
        }

        public override void Clear()
        {
            timerPool?.Clear();
            timerPool = null;
        }

        public override void Restar()
        {
            if (timerPool == null) return;
            for (int i = 0; i < timerPool.Count; i++)
            {
                timerPool[i].Restart();
            }
        }

        public void AddTimer(Timer timer)
        {
            timerPool.Add(timer);
        }

        public void RemoveTimer(Timer timer)
        {
            if (timerPool.Contains(timer)) {
                timerPool.Remove(timer);
            }
        }

        public void RemoveTimer(string id){
            Timer timer = GetTimer(id);
            if (timer != null && timerPool.Contains(timer))
                timerPool.Remove(timer);
        }

        public void Pause(Timer timer)
        {
            if (timerPool.Contains(timer))
            {
                timer.state = ETimerState.PAUSE;
            }
        }

        public void UnPause(Timer timer)
        {
            if (timerPool.Contains(timer))
            {
                timer.state = ETimerState.PLAYING;
            }
        }

        public Timer GetTimer(string id)
        {
            Timer timer = timerPool.Find(v => v.id == id);
            return timer;
        }

        /// <summary>
        /// 是否仍在计时
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsKeepTime(string id)
        {
            Timer timer = timerPool.Find(v => v.id == id);
            if (timer == null)
                return false;
            return true;
        }

        private void UpdateTimer()
        {
            if (timerPool == null || timerPool.Count == 0)
                return;

            for (int i = timerPool.Count - 1; i >= 0; i--)
            {
                Timer timer = timerPool[i];
                if (timer.state == ETimerState.PAUSE)
                    continue;
                if (timer.type == ETimerType.FIXED)
                    continue;
                timer.KeepRealTime();
                timer.update?.Invoke(timer.VirtualTime2TimeSpan());
                if (timer.time <= 0)
                {
                    timer.complete?.Invoke();
                    RemoveTimer(timer);
                }
            }
        }

        private void FixedUpdateTimer()
        {
            if (timerPool == null || timerPool.Count == 0)
                return;

            for (int i = timerPool.Count - 1; i >= 0; i--)
            {
                Timer timer = timerPool[i];
                if (timer.state == ETimerState.PAUSE)
                    continue;
                if (timer.type == ETimerType.REAL)
                    continue;
                timer.KeepFixedTime();
                timer.update?.Invoke(timer.VirtualTime2TimeSpan());
                if (timer.time <= 0)
                {
                    timer.complete?.Invoke();
                    RemoveTimer(timer);
                }
            }
        }
    }

}